package cn.edu.thsis.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.thsis.pojo.User;
import cn.edu.thsis.pojo.student;
import cn.edu.thsis.pojo.thsis;
import cn.edu.thsis.service.UserService;
import cn.edu.thsis.service.impl.UserServiceImpl;


@WebServlet("/TeaSvlt")
public class TeaSvlt extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	UserService tea = new UserServiceImpl();
	UserService stu = new UserServiceImpl();
	UserService th = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;utf-8");
		String tea_id = req.getParameter("uid");
		String thsis_id = req.getParameter("thsis_id");
		String oper = req.getParameter("oper");
		String stu_id = req.getParameter("stu_id");
		student stud = null;
		ResultSet rs = null;
		if ("accept".equals(oper)) {
			try {
				doAccept(req,resp,thsis_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if ("addth".equals(oper)) {
			addth(req,resp);
		}else if ("ok".equals(oper)) {
			try {
				ok(req,resp,thsis_id,stu_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if ("no".equals(oper)) {
			try {
				no(req,resp,thsis_id,stu_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else if ("myth".equals(oper)) {
			try {
				myth(req,resp);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if ("result".equals(oper)) {
			try {
				showresult(req,resp,thsis_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	
	//查看自己的判定结果
	private void showresult(HttpServletRequest req, HttpServletResponse resp, String thsis_id) throws SQLException, ServletException, IOException {
		List<student> stud =stu.doAcceptService(thsis_id);
		req.setAttribute("stud", stud);
		req.setAttribute("thsis_id", thsis_id);
		req.getRequestDispatcher("teacher/tea_stu2.jsp").forward(req, resp);
	}


	//拒绝学生选择该论题
	private void no(HttpServletRequest req, HttpServletResponse resp, String thsis_id, String stu_id) throws SQLException, ServletException {
		int index = th.noService(thsis_id,stu_id);
		if (index>0) {
			try {
				req.getRequestDispatcher("/TeaSvlt?oper=result").forward(req, resp);
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	//教师查询自己的论题
	private void myth(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
		User u = (User) req.getSession().getAttribute("user");
		HttpSession session = req.getSession();
		String uid = u.getUid();
		List<thsis> mythesis= th.myService(uid);
		req.setAttribute("mythesis",mythesis);
		req.getRequestDispatcher("teacher/tea_mythes.jsp").forward(req, resp);
	}


	//接受学生选择该论题
	private void ok(HttpServletRequest req, HttpServletResponse resp, String thsis_id, String stu_id) throws SQLException, ServletException {
		int index = th.okService(thsis_id,stu_id);
		if (index>0) {
			try {
				req.getRequestDispatcher("/TeaSvlt?oper=result").forward(req, resp);
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	//教师添加论题
	private void addth(HttpServletRequest req, HttpServletResponse resp) {
		String thsis_id = req.getParameter("thsis_id");
		String thsis_name = req.getParameter("thsis_name");
		String thsis_kind = req.getParameter("thsis_kind");
		String thsis_class = req.getParameter("thsis_class");
		User u = (User) req.getSession().getAttribute("user");
		HttpSession session = req.getSession();
		String uid = u.getUid();
		thsis the = new thsis();
		the.setTea_id(uid);
		the.setThsis_id(thsis_id);
		the.setThsis_name(thsis_name);
		the.setThsis_kind(thsis_kind);
		the.setThsis_class(thsis_class);
		int index = th.addthService(the);
		if (index>0) {
			try {
				resp.sendRedirect("teacher/tea_addth.jsp");
			} catch (IOException e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}


	//根据论题ID，得到预约该论题的学生信息
	private void doAccept(HttpServletRequest req, HttpServletResponse resp, String thsis_id) throws SQLException, ServletException, IOException {
		List<student> stud =stu.doAcceptService(thsis_id);
		req.setAttribute("stud", stud);
		req.setAttribute("thsis_id", thsis_id);
		req.getRequestDispatcher("teacher/my_stu.jsp").forward(req, resp);
	}


	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
